package cn.com.zd.io.util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;
import java.text.NumberFormat;
import java.util.*;

public class DataGeneratorUtils {
    private static final String[] COUNTRIES = {
            "中国", "美国", "德国", "法国", "日本", "英国", "印度", "巴西", "俄罗斯", "澳大利亚"
    };

    private static final String[] COUNTRY_CODES = {
            "CN", "US", "DE", "FR", "JP", "GB", "IN", "BR", "RU", "AU"
    };

    private static final String[] CITIES = {
            "北京", "纽约", "柏林", "巴黎", "东京", "伦敦", "德里", "圣保罗", "莫斯科", "悉尼"
    };

    private static final String ALPHANUMERIC_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    private static final Random random = new Random();

    private static List<String> secondNames = new ArrayList<>();
    static {

        try(FileReader fr = new FileReader(DataGeneratorUtils.class.getResource("/secondname.txt").getFile());
            BufferedReader br = new BufferedReader(fr)) {
            String data = br.readLine();
            while (data != null) {
                secondNames.addAll(Arrays.asList(data.split(",")));
                data = br.readLine();
            }
        }catch (Exception e){

        }
        System.out.println("一共加载姓氏："+secondNames.size());
    }
    /**
     * 数字格式化
     *
     * @param num
     * @return
     */
    public static String formate(long num) {
        NumberFormat numberFormat = NumberFormat.getInstance(Locale.getDefault());
        return numberFormat.format(num);
    }

    /**
     * 随机生成手机号
     *
     * @return
     */
    public static String generateRandomMobile() {
        // 手机号以1开头，第二位可以是3、4、5、6、7、8、9
        StringBuilder mobile = new StringBuilder("1");
        int secondDigit = random.nextInt(7) + 3; // 3 - 9
        mobile.append(secondDigit);

        // 接下来的9位随机数字
        for (int i = 0; i < 9; i++) {
            mobile.append(random.nextInt(10)); // 0 - 9
        }

        return mobile.toString();
    }

    /**
     * 随机生成电话号码
     *
     * @return
     */
    public static String generateRandomPhoneNumber() {
        // 假设区号为3位数字，电话为8位数字
        String areaCode = String.format("%03d", random.nextInt(1000)); // 000 - 999
        String telephone = String.format("%08d", random.nextInt(100000000)); // 00000000 - 99999999
        return areaCode + "-" + telephone;
    }


    /**
     * 随机生成的国家
     *
     * @return
     */
    public static String generateRandomCountry() {
        int index = random.nextInt(COUNTRIES.length);
        return COUNTRIES[index];
    }

    /**
     * 随机生成的国家代码
     *
     * @param country
     * @return
     */
    public static String getCountryCode(String country) {
        for (int i = 0; i < COUNTRIES.length; i++) {
            if (COUNTRIES[i].equals(country)) {
                return COUNTRY_CODES[i];
            }
        }
        return null;
    }

    /**
     * 随机生成的城市
     *
     * @return
     */
    public static String generateRandomCity() {
        int index = random.nextInt(CITIES.length);
        return CITIES[index];
    }

    public static int getRandomSex() {
        return new Random().nextInt(10) % 3;
    }

    public static String getRandomName() {
        return secondNames.get(random.nextInt(secondNames.size())) + getRandomChar(random.nextInt(3)+1);
    }
    public static String getRandomChar(int numChars) {
        StringBuilder result = new StringBuilder();
        Random random = new Random();

        // 汉字的Unicode范围
        int start = 0x4E00;
        int end = 0x9FA5;

        for (int i = 0; i < numChars; i++) {
            // 随机生成汉字
            char chineseChar = (char) (random.nextInt(end - start + 1) + start);
            result.append(chineseChar);
        }

        return result.toString();
    }

    // 生成随机身份证号码
    public static String generateIdCard() {
        StringBuilder idCard = new StringBuilder();

        // 1. 生成地址编码（前6位）
        idCard.append(generateRandomAddressCode());

        // 2. 生成出生日期（8位：yyyyMMdd）
        idCard.append(generateRandomBirthDate());

        // 3. 生成顺序码（1位）
        idCard.append(generateRandomSequenceCode());

        // 4. 计算校验码（最后1位）
        idCard.append(calculateCheckDigit(idCard.toString()));

        return idCard.toString();
    }

    private static String generateRandomAddressCode() {
        Random random = new Random();
        return String.format("%06d", random.nextInt(1000000)); // 随机生成6位数字
    }

    private static String generateRandomBirthDate() {
        Random random = new Random();
        int year = random.nextInt(100) + 1920; // 随机年份1920-2019
        int month = random.nextInt(12) + 1; // 随机月份1-12
        int day = random.nextInt(28) + 1; // 随机日期1-28 (简化处理)

        // 格式化为yyyyMMdd
        return String.format("%04d%02d%02d", year, month, day);
    }

    private static String generateRandomSequenceCode() {
        Random random = new Random();
        return String.format("%01d", random.nextInt(10)); // 随机生成1位数字
    }

    private static char calculateCheckDigit(String idCardWithoutCheckDigit) {
        int[] weights = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}; // 权重
        char[] checkDigits = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'}; // 校验码
        int sum = 0;

        for (int i = 0; i < idCardWithoutCheckDigit.length(); i++) {
            sum += (idCardWithoutCheckDigit.charAt(i) - '0') * weights[i];
        }

        return checkDigits[sum % 11];
    }

    public static String generateRandomString(int length) {

        Random random = new Random();
        StringBuilder sb = new StringBuilder(length);

        for (int i = 0; i < length; i++) {
            int index = random.nextInt(ALPHANUMERIC_CHARS.length());
            sb.append(ALPHANUMERIC_CHARS.charAt(index));
        }

        return sb.toString();
    }
}
